// 处理页面权限支撑配置
import { authHandlerInterface } from "./auth.interface";
import { useStore as useMenuStore } from "@/store/menu";
import { useStore as useUserStore } from "@/store/user";
import { judgeAuth } from "@/auth/checkAuth";
import Auth, { AuthTips } from "./auth";
export default (e: authHandlerInterface) => {
  const menuStore = useMenuStore();
  const userStore = useUserStore();
  const all = menuStore.state?.all || [];
  const routeInfo = all?.find?.(x => x?.path === e?.name) || {};
  let children = all?.filter(x => x?.parent_id === routeInfo?.id) || [];
  // 不是超级管理员时做权限处理
  if (!userStore.isSuperAdmin) {
    for (let x in e.handler) {
      // isBorrow:借权，权限不在直接上级下
      if (e?.handler?.[x]?.isBorrow) {
        e.handler[x].hasAuth = all?.findIndex(y => y.menu_name === e.handler[x].name) > -1 ? judgeAuth(all?.find(y => y.menu_name === e.handler[x].name).perms) : false;
      } else {
        e.handler[x].hasAuth = children?.findIndex(y => y.menu_name === e.handler[x].name) > -1 ? judgeAuth(children?.find(y => y.menu_name === e.handler[x].name).perms) : false;
      }
      if (e?.handler?.[x]?.mixin) {
        e.handler[x].mixin = (E: any) => {
          return <Auth auth={children?.find(y => y.menu_name === e.handler?.[x]?.name)?.perms} v-slots={{
            tips: () => <AuthTips message={e?.handler?.[x]?.noAuthTips || '暂无权限'} />
          }}>
            <E />
          </Auth>
        }
      }
    }
  }
  return e;
}